package com.itcast.spring6.aop.xml;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.Random;

/**
 * @author zhangtian105
 * @date 2023/5/11
 * @apiNote
 */
@Slf4j
public class AspectXmlConfig {
    public Object aroundPublicMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("@Around spring6.AspectXmlConfig.aroundPublicMethod ......, {}", joinPoint.getSignature()) ;

        Random random = new Random() ;
        // 抛出异常模拟出异常通知After-Throwing
        /*if (random.nextBoolean()) {
            throw new RuntimeException("For purpose XML configuration") ;
        }*/

        Long start = System.currentTimeMillis() ;
        Object process = joinPoint.proceed() ;

        log.info("执行耗时: {}", System.currentTimeMillis() - start);

        return process ;
    }

    public void finalizeAnyPublicMethod() {
        log.info("@After spring6.AspectXmlConfig.finalizeAnyPublicMethod ......") ;
    }

    public void beforeAnyPublicMethod(JoinPoint jointPoint) {
        log.info("@Before spring6.AspectXmlConfig.beforeAnyPublicMethod ......") ;
    }

    public void afterAnyPublicMethod(JoinPoint joinPoint,Object result) {
        log.info("@After-Return spring6.AspectXmlConfig.afterAnyPublicMethod ...... {}", result) ;
    }

    public void afterThrowingAnyPublicMethod(JoinPoint jointPoint,Throwable throwable) {
        log.info("@After-Throwing spring6.AspectXmlConfig.afterThrowingAnyPublicMethod ......") ;
    }
}
